<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/21
 * Time: 9:23
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class HomeTemplateModel
 * @package SDXApi\Model
 */
class HomeTemplateModel extends Model
{

	/**
	 * @var string
	 */
	protected $tableName = 'home_template';


	/**
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	public function find_home_template_for_status_by_id($id)
	{
		$time = time();

		$where = array(
//			'id'	=>	$id,
			'status'=>	1,
			'start_time'=>	array('LT', $time,),
			'end_time'	=>	array('GT', $time,),
		);

		return $this->where($where)->find();
	}

	/**
	 * 获取分组信息
	 * Created by Jerry
	 * @param $groupId
	 * @param int $isShow
	 * @return mixed
	 */
	public function find_home_template_by_group_id($groupId, $isShow = 0)
	{
		$where = array(
			'id'	=>	$groupId,
			'status'=>	1,
		);

		if ($isShow == 1) {
			unset($where['status']);
		}

		return $this->where($where)->find();
	}

	/**
	 * 返回该分组下当前启用的模块
	 * Created by Jerry
	 * @param $parentId
	 * @param int $isShow
	 * @return mixed
	 */
	public function find_home_template_by_parent_id($parentId, $isShow = 0)
	{

		$time = time();

		$where = array(
			'parent_id'	=>	$parentId,
			'status'	=>	1,
			'start_time'=>	array('LT', $time,),
			'end_time'	=>	array('GT', $time,),
		);

		if ($isShow == 1) {
			unset($where['status'], $where['start_time'], $where['end_time']);
		}

		return $this->where($where)->find();
	}

	/**
	 * 专辑
	 * Created by Jerry
	 * @param $template_id
	 * @return mixed
	 */
	public function find_home_template_album_by_template_id($template_id)
	{
		$time = time();
		$where = array(
			'sa.template_id'	=>	$template_id,
			'a.start_time'		=>	array('LT', $time,),
			'a.end_time'		=>	array('GT', $time,),
		);

		$field = 'a.id, a.name, a.small, a.description, sa.sort';

		$data = $this
			->table('sdx_home_template_stencil_album')
			->alias('sa')
			->field($field)
			->join('LEFT JOIN __ALBUM__ AS a ON sa.album_id = a.id')
			->where($where)
			->group('sa.album_id')
			->select();

		return $data;
	}

    /**
     * @author Jerry
     * @param $parentId
     * @return mixed
     */
	public function getTemplateLastData($parentId)
    {
        $where = array(
            'parent_id' =>  $parentId,
        );

        return $this->where($where)->order('sort desc')->find();
    }

}